<template>
  <div class="goodsContain">
    <!-- 小球 -->
    <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
      <div class="ball" v-show="ballFlag" ref="ball"></div>
    </transition>

    <!-- 商品轮播图区域 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <swiper :lunbotuList="lunbotu" :isfull="false"></swiper>
        </div>
      </div>
    </div>

    <!-- 商品购买区域 -->
    <div class="mui-card">
      <div class="mui-card-header">{{goodsinfo.name}}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="price">
            市场价：
            <del>￥{{goodsinfo.originalPrice}}</del>&nbsp;&nbsp;销售价：
            <span class="now_price">￥{{goodsinfo.minPrice}}</span>
          </p>
          <p>
            购买数量：
            <numbox @getcount="getCount" :max="goodsinfo.stores"></numbox>
          </p>
          <p>
            <mt-button type="primary" size="small">立即购买</mt-button>
            <mt-button type="danger" size="small" @click="addShopCar">加入购物车</mt-button>
          </p>
        </div>
      </div>
    </div>

    <!-- 商品参数区域 -->
    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品货号：{{goodsinfo.id}}</p>
          <p>库存情况：{{goodsinfo.stores}}</p>
          <p>上架时间：{{goodsinfo.dateAdd | dataFormat}}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary" size="large" plain @click="goodsDisc(id)">图文介绍</mt-button>
        <mt-button type="danger" size="large" plain @click="goodsComment(id)">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>

<script>
import swiper from "../subcomponents/swiper.vue";
import numbox from "../subcomponents/goodsinfo_numberbox.vue";

export default {
  data() {
    return {
      name: "",
      id: this.$route.params.id,
      lunbotu: [],
      goodsinfo: [],
      ballFlag: false,
      selectCount: 1
    };
  },
  created() {
    this.getLunbotu();
    this.getGoodsInfo();
  },
  methods: {
    getLunbotu() {
      this.$http.get("https://api.it120.cc/pp/banner/list").then(result => {
        if (result.body.code === 0) {
          // 增加一个属性
          // result.body.data.forEach(element => {
          //   element.img = element.picRL
          // });

          lunbotuList = result.body.data;
          for (var i = 0; i < lunbotuList.length; i++) {
            type = lunbotuList[i].title;
            console.log(this.id);
            if (type == this.id) {
              // this.lunbotu = lunbotuList[i];
              this.lunbotu.push(lunbotuList[i]);
            }
          }
        }
      });
    },

    getGoodsInfo() {
      this.$http
        .get("https://api.it120.cc/lisa/shop/goods/detail?id=" + this.id)
        .then(result => {
          if (result.body.code === 0) {
            this.goodsinfo = result.body.data.basicInfo;
          }
        });
    },

    goodsDisc(id) {
      this.$router.push({ name: "goodsdisc", params: { id } });
    },

    goodsComment(id) {
      this.$router.push({ name: "goodscomment", params: { id } });
    },

    addShopCar() {
      // 添加到购物车
      this.ballFlag = !this.ballFlag;

      // { id: 商品id, count: 要购买的数量, price: 商品单价, selected: false}
      var goodsinfo = {
        id: this.id,
        title: this.goodsinfo.name,
        count: this.selectCount,
        price: this.goodsinfo.minPrice,
        pic: this.goodsinfo.pic,
        selected: true
      };

      this.$store.commit("addToCar", goodsinfo);
    },

    beforeEnter(el) {
      el.style.transform = "translate(0,0)";
    },
    enter(el, done) {
      el.offsetWidth;

      // 获取 小球 坐标
      const ballPosition = this.$refs.ball.getBoundingClientRect();
      // 获取 徽标 坐标
      const badgePosition = document
        .getElementById("badge")
        .getBoundingClientRect();

      const xDist = badgePosition.left - ballPosition.left;
      const yDist = badgePosition.top - ballPosition.top;

      // el.style.transform = `translate(86px,231px)`;
      el.style.transform = `translate(${xDist}px,${yDist}px)`;
      el.style.transition = "all 0.5s cubic-bezier(0.4,-0.3,1,0.68)";

      done();
    },
    afterEnter(el) {
      this.ballFlag = !this.ballFlag;
    },

    getCount(count) {
      this.selectCount = count;
      // console.log("从父组件中拿到的值：" + this.selectCount);
    }
  },

  components: {
    swiper,
    numbox
  }
};
</script>

<style lang="scss" scoped>
.goodsContain {
  background-color: #eee;
  overflow: hidden;

  .now_price {
    font-size: 16px;
    font-weight: bold;
    color: red;
  }

  .mui-card-footer {
    display: block;
    button {
      margin: 15px 0;
    }
  }

  .ball {
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background-color: red;
    position: absolute;
    z-index: 99;
    top: 390px;
    left: 152px;
  }
}
</style>